package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.UUID;


import com.oocl.util.DBUtil;
import com.oocl.dao.MerchantDao;
import com.oocl.pojo.Merchant;

public class MerchantDaoImpl implements MerchantDao {

	@Override
	public int addMerchant(Merchant m) {
		String sql="insert into Merchant(mid,mname,mpwd,midcardimg,mheadimg,maddr,mstatus)values(?,?,?,?,?,?,?)";
		Connection conn=null;
		PreparedStatement pst =null;
		StringBuilder sb= new StringBuilder();
		UUID uuid = null;
		int n=0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, m.getmId());
			pst.setString(2, m.getmName());
			pst.setString(3, m.getmPwd());
			pst.setString(4, m.getmIdCardImg());
			pst.setString(5, m.getmHeadImg());
			pst.setString(6, m.getmAddr());
			pst.setInt(7, m.getmStatus());
			n = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(null, pst, conn);
		}
		return n;
	}

	@Override
	public Merchant findOneMerchant(String mName) {
		String sql="select * from merchant where mname = ?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Merchant m = null;
		String favsS= null;
		int n = 0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, mName);
			rs=pst.executeQuery();
			while(rs.next()){
				m = new Merchant();
				m.setmId(rs.getString("mid"));
				m.setmName(rs.getString("mname"));
				m.setmPwd(rs.getString("mpwd"));
				m.setmIdCardImg(rs.getString("midcardimg"));
				m.setmHeadImg(rs.getString("mheadimg"));
				m.setmAddr(rs.getString("maddr"));
				m.setmStatus(rs.getInt("mstatus"));
			}
			n=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return m;
	}

	@Override
	public int updateMerchant(Merchant m) {
		String sql="update merchant set mname=?,mpwd=?,midcardimg=?,mheadimg=?,maddr=?,mstatus=? where mid =?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		StringBuilder sb= new StringBuilder();
		int n=0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, m.getmName());
			pst.setString(2, m.getmPwd());
			pst.setString(3, m.getmIdCardImg());
			pst.setString(4, m.getmHeadImg());
			pst.setString(5, m.getmAddr());
			pst.setInt(6, m.getmStatus());
			pst.setString(7, m.getmId());
			n = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(null, pst, conn);
		}
		return n;
	}


}
